//
//  JsonService.m
//  iSale
//
//  Created by liusanchun on 14-9-16.
//  Copyright (c) 2014年 dyqy. All rights reserved.
//

#import "JsonService.h"

@implementation JsonService

/**
 *  将json字符串转换为NSDictionary或NSArray
 *
 *  @param jsonString json字符串
 *
 *  @return NSDictionary或NSArray
 */
+ (id)jsonObjectWithString:(NSString*)jsonString
{
    if ([jsonString isEqualToString:@""]
        || jsonString == NULL) {
        return nil;
    }
    NSError *error;
    NSLog(@"xhw->JSONService->return->jsonString->%@",jsonString);
    id object = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
                                                options:NSJSONReadingMutableLeaves
                                                error:&error];
    NSLog(@"xhw->JSONService->return->object->%@",object);
    return object;
}

/**
 *  将字典转换为json字符串
 *
 *  @param userInfo 字典
 *
 *  @return json字符串
 */
+ (NSString*)stringWithJsonObject:(NSMutableDictionary*)userInfo
{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo
                                                       options:0
                                                         error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                 encoding:NSUTF8StringEncoding];
    
//    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\\""
//                                                       withString:@"\""];
    return jsonString;
}

@end
